// user/pages/collect/index.js
import {
  userFollow,
  delcollect
} from '../../../utils/api'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totals: 0,
    list: [],
    showNull: false,
    showModal: false,
    page: 1,
    imgUrl: app.globalData.imgUrl,
    wxImgUrl: app.globalData.wxImgUrl,
    navBarHeight: '', //头高度
    statusBarHeight: '', //状态栏高度
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let navBarHeightinfo = app.publicFun.navBarHeightinfo()
    this.setData({
      navBarHeight: navBarHeightinfo[0],
      statusBarHeight: navBarHeightinfo[1]
    })
    this.getlist()
  },
  back() {
    wx.navigateBack({})
  },

  getlist() {
    wx.showLoading({
      title: '加载中...',
    })
    userFollow({
      pageNum: this.data.page,
      pageSize: 10,
      userId: wx.getStorageSync('userid')
    }).then(res => {
      wx.hideLoading()
      if (res.total == 0) {
        this.setData({
          showNull: true
        })
      }
      if (this.data.page > 1) {
        this.data.list = this.data.list.concat(res.records)
      } else {
        this.data.list = res.records
      }
      this.setData({
        totals: res.total,
        list: this.data.list
      })
    })
  },
  handlerInfo(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/index?id=${id}`,
    })
  },
  handlerDel(e) {
    this.setData({
      showModal: true,
      id: e.currentTarget.dataset.id
    })
  },
  noSure() {
    this.setData({
      showModal: false
    })
  },
  sure() {
    this.setData({
      showModal: false
    })
    delcollect({
      articleId: this.data.id,
      userId: wx.getStorageSync('userid')
    }).then(res => {
      if (res.code == undefined) {
        wx.showToast({
          title: '取消成功',
          icon: 'none'
        });
        this.getlist()
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
  onReachBottom: function () {
    if (this.data.list.length < this.data.totals) {
      this.data.page++;
      this.setData({
        page: this.data.page
      })
      this.getlist()
    }

  },

})